package com.armrest.screen

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.media.MediaPlayer
import android.view.View
import android.widget.ImageView


fun <T> List<T>.getOrThrow(index: Int): T? {
    return { this.getOrNull(index) }.excuteWithExcetpion(null)
}
/**
 *
 * @receiver (() -> T)
 * @param defalut T
 * @return T
 */
fun <T> (() -> T).excuteWithExcetpion(defalut: T? = null): T? {
    try {
        return this.invoke() ?: defalut
    } catch (e: Exception) {
        e.printStackTrace()
        return defalut
    }
}

/**
 * 动画无限旋转
 */
fun ImageView.setObjectAnimator(){
    val animator = ObjectAnimator.ofFloat(this, "rotation", 0f, 10f)
    animator.duration = 1000
    animator.repeatCount = ValueAnimator.INFINITE
    animator.repeatMode = ObjectAnimator.REVERSE
    animator.start()
}
fun View.setMediaPlayer(){
   var mediaPlayer= MediaPlayer.create(this.context, R.raw.didididi)
    mediaPlayer.setOnCompletionListener { _ ->
        mediaPlayer?.let {
            it.stop()
            it.release()
            mediaPlayer = null
        }
    }
    mediaPlayer.start()
}

fun View.setMediaPlayerMP3(){
    var mediaPlayer= MediaPlayer.create(this.context, R.raw.dijiadj)
    mediaPlayer.setOnCompletionListener { _ ->
        mediaPlayer?.let {
            it.stop()
            it.release()
            mediaPlayer = null
        }
    }
    mediaPlayer.start()
}

